/*
 * Mar 23, 2013
 */
package net.dntuan.simplexmlbundle.internal;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.simpleframework.xml.transform.Transform;

/**
 * @author tuandn
 * 
 */
public class HttpDateTransformer implements Transform<Date> {

    private DateFormat dateFormat;

    public HttpDateTransformer(DatePattern pattern) {
        if (pattern == null) {
            throw new RuntimeException("Pattern is not given yet!");
        }
        dateFormat = new SimpleDateFormat(pattern.toString());
    }

    @Override
    public Date read(String value) throws Exception {
        return dateFormat.parse(value);
    }

    @Override
    public String write(Date value) throws Exception {
        return dateFormat.format(value);
    }

}
